package com.applink.sell_proof.database.table;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.Redeem;

public class RedeemDBHelper {
	public static final String TABLE_NAME = "redeem_table";
	public static final String REDEEM_ID = "redeem_code";
	public static final String CAMPAIGN_NAME = "campaign_name";
	public static final String REDEEM_DATE = "redeem_date";
	public static final String REDEEM_START_DATE = "redeem_start_date";
	public static final String REDEEM_END_DATE = "redeem_end_date";
	public static final String REDEEM_POINT = "redeem_point";
	public static final String REDEEM_PRODUCT = "redeem_product";
	public static final String PROGRAM_NAME = "program_name";
	public static final String MAX_ELIGIBLE = "max_eligible";
	public static final String MIN_ELIGIBLE = "min_eligible";
	public static final String FIRST_NAME = "first_name";
	public static final String LAST_NAME = "last_name";
	public static final String MOBILE = "mobile";
	public static final String EMAIL = "email";
	public static final String COUNTRY_ID = "country_id";
	public static final String POSTAL_CODE = "postal_code";
	public static final String STREET = "street";
	public static final String BUILDING = "building";
	public static final String UNIT = "unit";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ REDEEM_ID + " integer primary key ," + CAMPAIGN_NAME + " text ,"
			+ REDEEM_DATE + " integer ," + REDEEM_START_DATE + " integer ,"
			+ REDEEM_END_DATE + " integer ," + REDEEM_POINT + " integer ,"
			+ REDEEM_PRODUCT + " text ," + PROGRAM_NAME + " text ,"
			+ MAX_ELIGIBLE + " integer ," + MIN_ELIGIBLE + " integer ,"
			+ FIRST_NAME + " text ," + LAST_NAME + " text ," + MOBILE
			+ " text ," + EMAIL + " text ," + COUNTRY_ID + " integer ,"
			+ POSTAL_CODE + " text ," + STREET + " text ," + BUILDING
			+ " text ," + UNIT + " text "

			+ ");";

	public static void createRedeem(final Redeem redeem) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(REDEEM_ID, redeem.getId());
				value.put(CAMPAIGN_NAME, redeem.getCampaignName());
				value.put(REDEEM_DATE, redeem.getDate());
				value.put(REDEEM_START_DATE, redeem.getStartdate());
				value.put(REDEEM_END_DATE, redeem.getEnddate());
				value.put(REDEEM_POINT, redeem.getPoint());
				value.put(REDEEM_PRODUCT, redeem.getProduct());
				value.put(PROGRAM_NAME, redeem.getProgramName());
				value.put(MAX_ELIGIBLE, redeem.getMaxEligible());
				value.put(MIN_ELIGIBLE, redeem.getMinEligble());
				value.put(FIRST_NAME, redeem.getFirstName());
				value.put(LAST_NAME, redeem.getLastName());
				value.put(MOBILE, redeem.getMobile());
				value.put(EMAIL, redeem.getEmail());
				value.put(COUNTRY_ID, redeem.getCountryId());
				value.put(POSTAL_CODE, redeem.getPostalCode());
				value.put(STREET, redeem.getStreet());
				value.put(BUILDING, redeem.getBuilding());
				value.put(UNIT, redeem.getUnit());
				resolver.insert(EMarketingContentProvider.REDEEM_CONTENT_URI,
						value);
			}

		}.start();
	}

	public static List<Redeem> getAllRedeem() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.REDEEM_CONTENT_URI, new String[] {
						REDEEM_ID, REDEEM_DATE, REDEEM_POINT, REDEEM_PRODUCT },
				null, null, null);

		if (cursor != null) {
			List<Redeem> redeems = new ArrayList<Redeem>();
			while (cursor.moveToNext()) {
				Redeem redeem = new Redeem();
				redeem.setId(cursor.getLong(cursor.getColumnIndex(REDEEM_ID)));
				redeem.setPoint(cursor.getInt(cursor
						.getColumnIndex(REDEEM_POINT)));
				redeem.setProduct(cursor.getString(cursor
						.getColumnIndex(REDEEM_PRODUCT)));
				redeem.setDate(cursor.getLong(cursor
						.getColumnIndex(REDEEM_DATE)));
				redeems.add(redeem);
			}
			return redeems.size() > 0 ? redeems : null;
		}
		return null;
	}

	public static Redeem getRedeemById(long id) {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();

		Cursor cursor = resolver.query(
				EMarketingContentProvider.REDEEM_CONTENT_URI, null, REDEEM_ID
						+ "=?", new String[] { Long.toString(id) }, null);

		if (cursor != null && cursor.moveToFirst()) {
			Redeem redeem = new Redeem();
			redeem.setId(id);
			redeem.setCampaignName(cursor.getString(cursor
					.getColumnIndex(CAMPAIGN_NAME)));
			redeem.setPoint(cursor.getInt(cursor.getColumnIndex(REDEEM_POINT)));
			redeem.setProduct(cursor.getString(cursor
					.getColumnIndex(REDEEM_PRODUCT)));
			redeem.setDate(cursor.getLong(cursor.getColumnIndex(REDEEM_DATE)));
			redeem.setStartdate(cursor.getLong(cursor
					.getColumnIndex(REDEEM_START_DATE)));
			redeem.setEnddate(cursor.getLong(cursor
					.getColumnIndex(REDEEM_END_DATE)));
			redeem.setProgramName(cursor.getString(cursor
					.getColumnIndex(PROGRAM_NAME)));
			redeem.setMaxEligible(cursor.getInt(cursor
					.getColumnIndex(MAX_ELIGIBLE)));
			redeem.setMinEligible(cursor.getInt(cursor
					.getColumnIndex(MIN_ELIGIBLE)));
			redeem.setFirstName(cursor.getString(cursor
					.getColumnIndex(FIRST_NAME)));
			redeem.setLastName(cursor.getString(cursor
					.getColumnIndex(LAST_NAME)));
			redeem.setMobile(cursor.getString(cursor.getColumnIndex(MOBILE)));
			redeem.setEmail(cursor.getString(cursor.getColumnIndex(EMAIL)));
			redeem.setCountryId(cursor.getInt(cursor.getColumnIndex(COUNTRY_ID)));
			redeem.setStreet(cursor.getString(cursor.getColumnIndex(STREET)));
			redeem.setBuilding(cursor.getString(cursor.getColumnIndex(BUILDING)));
			redeem.setUnit(cursor.getString(cursor.getColumnIndex(UNIT)));

			cursor.close();
			return redeem;
		}
		return null;
	}

}
